package controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import gameapi.Game;
import gameapi.Player;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;

import db.RegisteredUserDb;

@ManagedBean
@SessionScoped
public class GameController {
	private RegisteredUserDb userDatabase = RegisteredUserDb.getInstance();
	//@ManagedProperty(value="#{game}")
	private Game game;
	private List<Player> playerToJoinList = new ArrayList<Player>();
	@ManagedProperty(value = "#{player}")
	private Player player;
	private static Logger logger = Logger.getLogger(GameController.class);
	

public GameController(){
	logger.debug("create gamecontroller");
	game = new Game();
}

public String restartGame(){
	playerToJoinList.clear();
	game = new Game();
	setPlayer(player);
	joinPlayer(player);
	player.setLoggedIn(false);
	player.setLastDiceResult(0);
	return "";
}

public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		logger.debug("setPlayer " + player.getUserName());
		this.player = player;
	}
	public void joinPlayer(Player player){
		playerToJoinList.add(player);
		playerToJoinList.add(userDatabase.getPlayer("Computer"));
		logger.debug("playerToJoinList created: " + playerToJoinList.size());
		getGame().joinPlayers(playerToJoinList, true);
		logger.debug("joined");
	}
	/**
	 * get player routs, get gameinfo get all :)
	 */
	
	public void setGame(Game game){
		this.game = game;
	}
	
	public Game getGame(){
		logger.debug(this.game);
		return this.game;
	}

	/**
	 * get player routs, get gameinfo get all :)
	 */
}
